package com.odm.communitymanage.ui.ServicePhone

import android.content.Intent
import android.content.pm.PackageManager
import android.net.Uri
import android.provider.Settings
import androidx.core.app.ActivityCompat
import androidx.core.content.ContextCompat
import com.blankj.utilcode.constant.PermissionConstants
import com.blankj.utilcode.util.IntentUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.PermissionUtils
import com.blankj.utilcode.util.ToastUtils
import com.odm.communitymanage.R
import com.odm.communitymanage.base.BaseFragment
import kotlinx.android.synthetic.main.fragment_service_phone.*
import java.util.jar.Manifest

/**
 * @description: 服务电话页面,，暂时固定页面的布局，后期改为滚动列表动态获取服务电话号码
 * @author: ODM
 * @date: 2020/4/14
 */
class ServicePhoneFragment : BaseFragment() {

    val MANIFST_CALL_CODE  = 1

    var selectedPhone = 0

    override fun initViews() {
        initPhoneCallEvents()

    }

    override fun getLayoutId(): Int {
        return R.layout.fragment_service_phone
    }

    /**
     * 初始化 拨打对应类型电话的点击事件
     */
    private fun initPhoneCallEvents(){
        rlSecurityRoom_ServicePhone.setOnClickListener {
            callByNumberString("35720999")
            selectedPhone = 35720999
        }
        rlCarry_ServicePhone.setOnClickListener {
            callByNumberString("37720998")
            selectedPhone = 37720998
        }
        rlEle_ServicePhone.setOnClickListener {
            callByNumberString("38720997")
            selectedPhone = 38720997

        }
        rlProperty_ServicePhone.setOnClickListener {
            callByNumberString("37720996")
            selectedPhone = 37720996
        }
        rlWater_ServicePhone.setOnClickListener {
            callByNumberString("35720995")
            selectedPhone = 35720995
        }
    }



    private  fun callByNumberString( phone : String) {
        if (ContextCompat.checkSelfPermission(requireContext() , android.Manifest.permission.CALL_PHONE) != PackageManager.PERMISSION_GRANTED){
            //首先判断否获取了权限
            if (ActivityCompat.shouldShowRequestPermissionRationale( requireActivity(),android.Manifest.permission.CALL_PHONE)) {
                //用户曾经拒绝过，让用户去应用设置页手动授权
                ToastUtils.showLong("请在权限管理中手动打开拨打电话权限")
                val intent =  Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS) 
                val uri = Uri.fromParts("package", requireActivity().packageName , null)
                intent.data = uri
                startActivity(intent) 
            }else{
                //用户没有拒绝过，正常申请权限
                LogUtils.d("正常申请权限")
                requestPermissions( arrayOf(android.Manifest.permission.CALL_PHONE),MANIFST_CALL_CODE)
            }
        }else {
            LogUtils.d("已有权限，直接拨打")
            //已有权限，直接拨打
            val intent =  Intent()
            intent.action = Intent.ACTION_CALL
            intent.data = Uri.parse("tel:$phone")
            startActivity(intent)
        }
    }

    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<out String>,
        grantResults: IntArray
    ) {
        if(requestCode == MANIFST_CALL_CODE ){
            if(grantResults != null && grantResults.isNotEmpty()) {
                if (grantResults.size == 1 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                    // Permission Granted
                    LogUtils.d("收到了权限回调")
                    val intent = Intent()
                    intent.action = Intent.ACTION_CALL
                    val phone = "tel:$selectedPhone"
                    intent.data = Uri.parse(phone)
                    startActivity(intent)
                } else if (grantResults.size == 1 && grantResults[0] == PackageManager.PERMISSION_DENIED) {
                    // Permission Denied
                    ToastUtils.showShort("拒绝电话权限将无法继续拨打电话")
                } else {
                    super.onRequestPermissionsResult(requestCode, permissions, grantResults);
                }
            }

        }
    }
}